using System.Collections.Generic;
using System.Globalization;
using EPiServer.Core;
using EPiServer.Filters;
using OpenWaves.EPiServer.Abstractions;

namespace OpenWaves.EPiServer
{
    public static class PageReferenceExtensions
    {
        public static PageData Resolve(this PageReference pageReference, CultureInfo preferredCulture = null)
        {
            if (preferredCulture != null)
                return ServiceLocator.Resolve<IDataFactory>().GetPage(pageReference, new LanguageSelector(preferredCulture.Name));
            else
                return ServiceLocator.Resolve<IDataFactory>().GetPage(pageReference);
        }

        public static IEnumerable<PageData> GetChildren(this PageReference pageReference, ILanguageSelector languageSelector = null)
        {
            return pageReference.GetChildren(new IPageFilter[] {new FilterPublished(), new FilterAccess()}, languageSelector);
        }

        public static IEnumerable<PageData> GetChildren(this PageReference pageReference, IEnumerable<IPageFilter> filters, ILanguageSelector languageSelector = null)
        {
            var dataFactory = ServiceLocator.Resolve<IDataFactory>();

            return dataFactory.GetChildren(pageReference, languageSelector).FilteredBy(filters);
        }
    }
}